#include "mysql_message.hpp"
#include "message.hxx"
#include "message-odb.hxx"
#include "uuid.hpp"

#include <gflags/gflags.h>

DEFINE_bool(log_mode, false, "程序的运行模式: true-发布模式 false-调试模式");
DEFINE_string(log_file, "", "发布模式下，制定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，制定日志的输出等级");

void insert(bite_im::MessageTable &table)
{
    boost::posix_time::ptime ctime1(boost::posix_time::time_from_string("2024-10-03 23:07:11.001"));
    bite_im::Message message1("消息id-1", "会话1", "2022044026", 0, boost::posix_time::time_from_string("2024-10-03 23:00:11.001"));
    message1.content("只因");
    bite_im::Message message2("消息id-2", "会话1", "785d-5804-911f-0001", 0, boost::posix_time::time_from_string("2024-10-03 23:02:11.001"));
    message2.content("你");
    bite_im::Message message3("消息id-3", "会话1", "2024044026", 0, boost::posix_time::time_from_string("2024-10-03 23:07:11.001"));
    message3.content("太");
    bite_im::Message message4("消息id-4", "会话1", "987a-80ee-ad9e-0000", 0, boost::posix_time::time_from_string("2024-10-03 23:11:11.001"));
    message4.content("美");
    bite_im::Message message5("消息id-5", "会话2", "2022044026", 0, boost::posix_time::time_from_string("2024-10-03 21:07:11.001"));
    message5.content("尊嘟假嘟");
    bite_im::Message message6("消息id-6", "会话2", "2024044026", 0, boost::posix_time::time_from_string("2024-10-03 22:07:11.001"));
    message6.content("尊嘟~假嘟");
    table.insert(message1);
    table.insert(message2);
    table.insert(message3);
    table.insert(message4);
    table.insert(message5);
    table.insert(message6);
}

void remove(bite_im::MessageTable &table)
{
    table.remove("会话1");
    table.remove("会话2");
}

void recent(bite_im::MessageTable &table)
{
    std::vector<bite_im::Message> message_list = table.recent("会话1", 3);
    for (int i = message_list.size() - 1; i >= 0; i--)
    {
        std::cout << message_list[i].user_id() << " " << message_list[i].content() << std::endl;
    }
}
void range(bite_im::MessageTable &table)
{
    boost::posix_time::ptime ctime1(boost::posix_time::time_from_string("2024-10-03 23:01:11.001"));
    boost::posix_time::ptime ctime2(boost::posix_time::time_from_string("2024-10-03 23:08:11.001"));
    std::vector<bite_im::Message> message_list = table.range("会话1", ctime1, ctime2);
    // for (int i = message_list.size() - 1; i >= 0; i--)
    // {
    //     std::cout << message_list[i].user_id() << " " << message_list[i].content() << std::endl;
    // }
    for(auto &message : message_list)
    {
        std::cout << message.user_id() << " " << message.content() << std::endl;
    }
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    bite_im::initLogger(FLAGS_log_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = bite_im::ODBFactory::create("root", "Czh432248", "ChatRoom",
                                          "127.0.0.1", 0, "utf8", 3);
    bite_im::MessageTable table(db);

    // insert(table);
    // remove(table);
    recent(table);
    range(table);
    return 0;
}